package cn.elead.chaos.sso.server.web.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.elead.chaos.sso.server.web.system.domain.SysUser;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * 登录密码方法
 * 
 * @author luopeng
 */
@Service
public class SysPasswordService {

	@Autowired
	private RSA rsa;

	public void validate(SysUser user, String password) {

	}

	public boolean matches(SysUser user, String newPassword) {
		return user.getPassword().equals(encryptPassword(newPassword));
	}

	public String encryptPassword(String password) {
		return HexUtil.encodeHexStr(rsa.encrypt(password, KeyType.PrivateKey));
	}
	
	public String decryptPassword(String password) {
		byte[] decrypt = rsa.decrypt(HexUtil.decodeHex(password), KeyType.PublicKey);
		return StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
	}

}
